package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.business.domain.VoucherOrder;
import com.ruoyi.business.domain.VoucherOrderDto;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.business.domain.Voucher;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.VoucherOrderMapper;
import com.ruoyi.business.service.VoucherOrderService;

/**

 * 优惠券订单Service业务层处理

 *

 * @author ruoyi

 * @date 2025-06-09
 */
@Service
public class VoucherOrderServiceImpl implements VoucherOrderService
{
    @Autowired
    private VoucherOrderMapper tbVoucherOrderMapper;

    /**

     * 查询优惠券订单
     *
     * @param id 优惠券订单主键
     * @return 优惠券订单
     */
    @Override
    public VoucherOrder selectTbVoucherOrderById(Long id)
    {
        return tbVoucherOrderMapper.selectTbVoucherOrderById(id);
    }

    /**

     * 查询优惠券订单列表
     *
     * @param voucherOrder 优惠券订单
     * @return 优惠券订单
     */
    @Override
    public List<VoucherOrderDto> selectTbVoucherOrderList(VoucherOrder voucherOrder,Long userId)
    {
        List<VoucherOrderDto> tbVoucherOrderDtoList = new ArrayList<>();
        List<VoucherOrder> list = tbVoucherOrderMapper.selectTbVoucherOrderList(voucherOrder,userId);
        for(VoucherOrder voucherOrderOne :list){
            VoucherOrderDto tbVoucherOrderDto = new VoucherOrderDto();
            String userIds= voucherOrderOne.getUserId();
            String voucherId= voucherOrderOne.getVoucherId();
            String userName = tbVoucherOrderMapper.selectTbUserNameById(userIds);
            String voucherName = tbVoucherOrderMapper.selectTbVoucherNameById(voucherId);
            SysUser sysUser = new SysUser();
            sysUser.setNickName(userName);
            Voucher voucher = new Voucher();
            voucher.setTitle(voucherName);
            tbVoucherOrderDto.setVoucher(voucher);
            tbVoucherOrderDto.setUser(sysUser);
            tbVoucherOrderDto.setId(voucherOrderOne.getId());
            tbVoucherOrderDto.setUserId(voucherOrderOne.getUserId());
            tbVoucherOrderDto.setVoucherId(voucherOrderOne.getVoucherId());
            tbVoucherOrderDto.setPayType(voucherOrderOne.getPayType());
            tbVoucherOrderDto.setStatus(voucherOrderOne.getStatus());
            tbVoucherOrderDto.setUseTime(voucherOrderOne.getUseTime());
            tbVoucherOrderDto.setPayTime(voucherOrderOne.getPayTime());
            tbVoucherOrderDto.setCreateTime(voucherOrderOne.getCreateTime());
            tbVoucherOrderDto.setRefundTime(voucherOrderOne.getRefundTime());
            tbVoucherOrderDto.setUpdateTime(voucherOrderOne.getUpdateTime());
            tbVoucherOrderDtoList.add(tbVoucherOrderDto);
        }
       return tbVoucherOrderDtoList;
    }

    /**

     * 新增优惠券订单
     *
     * @param voucherOrder 优惠券订单
     * @return 结果
     */
    @Override
    public int insertTbVoucherOrder(VoucherOrder voucherOrder)
    {
        voucherOrder.setCreateTime(DateUtils.getNowDate());
        return tbVoucherOrderMapper.insertTbVoucherOrder(voucherOrder);
    }

    /**

     * 修改优惠券订单
     *
     * @param voucherOrder 优惠券订单
     * @return 结果
     */
    @Override
    public int updateTbVoucherOrder(VoucherOrder voucherOrder)
    {
        voucherOrder.setUpdateTime(DateUtils.getNowDate());
        return tbVoucherOrderMapper.updateTbVoucherOrder(voucherOrder);
    }

    /**

     * 批量删除优惠券订单
     *
     * @param ids 需要删除的优惠券订单主键
     * @return 结果
     */
    @Override
    public int deleteTbVoucherOrderByIds(Long[] ids)
    {
        return tbVoucherOrderMapper.deleteTbVoucherOrderByIds(ids);
    }

    /**

     * 删除优惠券订单信息
     *
     * @param id 优惠券订单主键
     * @return 结果
     */
    @Override
    public int deleteTbVoucherOrderById(Long id)
    {
        return tbVoucherOrderMapper.deleteTbVoucherOrderById(id);
    }
}